package com.wxq.workpad.widget;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowInsets;
import android.widget.FrameLayout;

/**
 * @author ArcherYc
 * @date on 2019-06-11  21:38
 * @mail 247067345@qq.com
 */
public class WindowInsertFrameLayout extends FrameLayout {

    public WindowInsertFrameLayout(Context context) {
        this(context,null);
    }

    public WindowInsertFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnHierarchyChangeListener(new OnHierarchyChangeListener() {
            @Override
            public void onChildViewAdded(View parent, View child) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
                    requestApplyInsets();
                }
            }

            @Override
            public void onChildViewRemoved(View parent, View child) {

            }
        });
    }

    @Override
    public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            for (int index = 0; index < getChildCount(); index++) {
                getChildAt(index).dispatchApplyWindowInsets(insets);
            }
        }
        return insets;
    }
}
